<html>
	<head>
		<title>
			Web web web
		</title>
	</head>
	<script src="../Orbited.js"></script>
	<script src="QueueCPP.js"></script>
	<script type="text/javascript">
		function initPage() {
			tcp = new Orbited.TCPSocket();
			DataRecieveQueue = new Queue();			
			DataSendQueue = new Queue();
			processEvent();
			GameState = 0;
			Username = undefined;
			UserMoney = 0;
			RoomList = "";
		}
		
		function connectServer() {
			//alert("Connect Server");
			if (tcp.readyState == 3) return;
			tcp.open('localhost', 8008, false);
			tcp.onopen = function() {
				//alert("Connection opened");
			}
			tcp.onread = function(data) { 
				DataRecieveQueue.push(data);				
			}
			tcp.onclose = function() {
				alert("Connection closed");
				goscene1();
				initPage();
			}
		}
		function disconnectServer() {
			//disable notify closed connection
			tcp.onclose = function() {				
				goscene1();
				initPage();
			}
			tcp.close();
			//location.reload(true);			
			//location.reload(true);
		}
		
		function send(data) {
			if (tcp.readyState < 3)
				alert("ERR: Not Connected");
			else if (tcp.readyState > 3 ) {
				alert("ERR: Disconnect(ed)(ing)");
			}
			else {				
				tcp.send(data);				
			}
	    }		
		
  		function userlogin(username,password) {
			//alert("Login login");
			var command = "login "+username+" "+password;
			Username = username;
			//send(command);
			DataSendQueue.push(command);
			goloading();
		}
		
		function getroomlist() {
			var command = "roomlist"
			DataSendQueue.push(command);
		}
		
		function goscene2() {
			document.getElementById('DivScene').innerHTML = "<iframe scrolling='no' frameborder='no' width='100%' height='500' src='page2.html'></iframe>";
		}
		
		function goscene1() {
			document.getElementById('DivScene').innerHTML = "<iframe scrolling='no' frameborder='no' width='100%' height='200' src='page1.html'></iframe>";
		}
		
		function goscene3() {
			document.getElementById('DivScene').innerHTML = "<iframe scrolling='no' frameborder='no' width='100%' height='700' src='page3.html'></iframe>";
		}
		
		function goloading() {
			document.getElementById('DivScene').innerHTML = "Loading ... Please wait.";
		}
		
		function joinRoom(room) {
			var command = "join "+room;
			DataSendQueue.push(command);
		}
		function createRoom() {
			var command = "create 100";
			DataSendQueue.push(command);
		}
		//infinite loop process game event
		function processEvent() {
			//process outgoing data
			if (tcp.readyState == 3) {
				while (DataSendQueue.size()>0) {
					data = DataSendQueue.pop();
					processSend(data);
				}
			}
			//process incoming data
			while (DataRecieveQueue.size()>0) {
				data = DataRecieveQueue.pop();
				processRecieve(data);
			}
			//repeat
			setTimeout("processEvent()",200);
		}
		function processSend(data) {
			send(data);
		}
		function processRecieve(data) {			
			var adata = data.split("\n");
			for (var xdata in adata) {
				var args = adata[xdata].split(" ");
				var command = args[0];
				if (command == "login_ok") {
					goscene2();
				}
				if (command == "login_error") {
					alert("Login failed.");
					goscene1();
				}
				if (command == "join_error") {
					alert("Join error");
				}
				if (command == "usermoney") {
					var money = args[2];
					var imoney = parseInt(money);
					var UserMoney = imoney;
					//alert(UserMoney);
				}
				if (command == "roomlist") {
					RoomList = "";
					for (var i = 1; i < args.length; i++) {
						RoomList = RoomList + args[i];
						if (i < args.length-1) {
							RoomList = RoomList + " ";
						}
					}
				}
				if (command == "join_ok") {
					goscene3();
				}
				if (command == "create_ok") {
					goscene3();
				}				
			}
		}
		
	</script>
	<body onload = "initPage();">
		<div id="DivScene">
			<iframe scrolling='no' frameborder='no' width='100%' height='300' src='page1.html'></iframe>
			<img src="static/jsclient/background.png" width="600" height="660"> </img>
		</div>
	</body>
</html>